
package com.hlkj.pay.service.order.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import com.hlkj.pay.dto.order.WithdrawPageStatisticsDto;
import com.hlkj.pay.vo.order.req.WithdrawOrderPageQueryReq;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.order.OrderWithdrawDto;
import com.hlkj.pay.dto.order.WithdrawOrderQueryDto;
import com.hlkj.pay.infrastructure.mapper.order.OrderWithdrawMapper;
import com.hlkj.pay.infrastructure.model.order.OrderWithdrawDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.order.IWithdrawOrderService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2025/02/08 15:27
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class WithdrawOrderServiceImpl extends CommonSnFilterService implements IWithdrawOrderService {

    private final OrderWithdrawMapper orderWithdrawMapper;

    @Override
    public OrderWithdrawDO queryPayOrder(String payOrderNo) {
        return orderWithdrawMapper.selectOne(OrderWithdrawDO::getPayOrderNo, payOrderNo);
    }

    @Override
    public int updatePayOrderStatus(OrderWithdrawDO updatePayOrderDO) {
        return orderWithdrawMapper.updateById(updatePayOrderDO);
    }

    @Override
    public OrderWithdrawDO queryByMchOrderNo(String sn, String mchOrderNo) {
        return orderWithdrawMapper.selectOne(OrderWithdrawDO::getSn, sn, OrderWithdrawDO::getMchOrderNo, mchOrderNo);
    }

    @Override
    public void addWithdrawOrder(OrderWithdrawDO orderWithdraw) {
        orderWithdrawMapper.insert(orderWithdraw);
    }

    @Override
    public PageResult<OrderWithdrawDto> queryPage(WithdrawOrderQueryDto commonOrderQueryDto) {
        commonOrderQueryDto.setSortingFields("po.id");
        IPage<OrderWithdrawDto> mpPage = MyBatisUtils.buildPage(commonOrderQueryDto);
        QueryWrapper<OrderWithdrawDto> queryWrapper = buildWrapper(commonOrderQueryDto);
        orderWithdrawMapper.selectPayPage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }

    @Override
    public WithdrawPageStatisticsDto withdrawStaticOrdersPage(WithdrawOrderQueryDto commonOrderQueryDto) {
        return orderWithdrawMapper.withdrawStaticOrdersPage(commonOrderQueryDto);
    }

    @Override
    public List<OrderWithdrawDO> queryList(WithdrawOrderQueryDto commonOrderQueryDto) {
        return orderWithdrawMapper.selectList(buildListWrapper(commonOrderQueryDto));
    }

    @Override
    public List<OrderWithdrawDto> queryPageList(WithdrawOrderQueryDto commonOrderQueryDto) {
        return orderWithdrawMapper.queryList(buildWrapper(commonOrderQueryDto));
    }

    /**
     * 查询条件封装
     *
     * @param withdrawOrderQueryDto
     * @return
     */
    QueryWrapper<OrderWithdrawDto> buildWrapper(WithdrawOrderQueryDto withdrawOrderQueryDto) {
        filterSn(withdrawOrderQueryDto);
        QueryWrapperX<OrderWithdrawDto> queryWrapper = new QueryWrapperX<>();
        queryWrapper.likeIfPresent("po.mch_order_no", withdrawOrderQueryDto.getMchOrderNo());
        queryWrapper.likeIfPresent("po.pay_order_no", withdrawOrderQueryDto.getPayOrderNo());
        queryWrapper.likeIfPresent("po.channel_order_no", withdrawOrderQueryDto.getChannelOrderNo());
        queryWrapper.eqIfPresent("po.sn", withdrawOrderQueryDto.getSn());
        queryWrapper.inIfPresent("po.sn", withdrawOrderQueryDto.getSnList());
        queryWrapper.eqIfPresent("po.app_id", withdrawOrderQueryDto.getAppId());
        queryWrapper.eqIfPresent("po.status", withdrawOrderQueryDto.getStatus());
        queryWrapper.eqIfPresent("po.provider_code", withdrawOrderQueryDto.getProviderCode());
        queryWrapper.eqIfPresent("po.channel_code", withdrawOrderQueryDto.getChannelCode());
        queryWrapper.eqIfPresent("po.notify_state", withdrawOrderQueryDto.getNotifyState());
        queryWrapper.geIfPresent("po.create_time", withdrawOrderQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent("po.create_time", withdrawOrderQueryDto.getEndQueryTime());
        queryWrapper.geIfPresent("po.amount", withdrawOrderQueryDto.getMinAmount());
        queryWrapper.leIfPresent("po.amount", withdrawOrderQueryDto.getMaxAmount());
        queryWrapper.likeIfPresent("po.user_id", withdrawOrderQueryDto.getUserId());
        queryWrapper.likeIfPresent("report.virtual_account_no", withdrawOrderQueryDto.getVirtualAccountNo());
        queryWrapper.likeIfPresent("report.name", withdrawOrderQueryDto.getUserName());
        return queryWrapper;
    }
    /**
     * 查询条件封装
     *
     * @param withdrawOrderQueryDto
     * @return
     */
    LambdaQueryWrapperX<OrderWithdrawDO> buildListWrapper(WithdrawOrderQueryDto withdrawOrderQueryDto) {
        filterSn(withdrawOrderQueryDto);
        LambdaQueryWrapperX<OrderWithdrawDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(OrderWithdrawDO::getStatus, withdrawOrderQueryDto.getStatus());
        queryWrapper.inIfPresent(OrderWithdrawDO::getStatus, withdrawOrderQueryDto.getStatusList());
        return queryWrapper;
    }
}
